package com.woniuxy.yoga.commons.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {

    /**
     * String转LocalDateTime类型
     * @param date String
     * @return LocalDateTime
     */
    public static LocalDateTime StringToLocalDateTime(String date){
        if (date==null) return null;
        String dateReg = "^\\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|10|20|3[0-1])$";
        String dateTimeReg = "^\\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|10|20|3[0-1]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
        if (date.matches(dateReg)) {
            return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }else if (date.matches(dateTimeReg)){
            return LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }else{
            throw new RuntimeException("日期格式不正确!");
        }
    }

    /**
     * 字符串转LocalDate类型
     * @param date String
     * @return LocalDate
     */
    public static LocalDate StringToLocalDate(String date){
        if (date==null) return null;
        String dateReg = "^\\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|10|20|3[0-1])$";
        String dateTimeReg = "^\\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|10|20|3[0-1]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        if (date.matches(dateReg)) {
            return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }else if (date.matches(dateTimeReg)){
            return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }else{
            throw new RuntimeException("日期格式不正确!");
        }
    }

    /**
     * 字符串转LocalTime类型
     * @param time String
     * @return LocalTime
     */
    public static LocalTime stringToLocalTime(String time){
        String regexp = "^([0-1][0-9]|2[0-3]):([0-5][0-9])$";
        String regexp1 = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

        if (time==null) return null;
        if (time.matches(regexp)){
            return LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"));
        } else if (time.matches(regexp1)) {
            return LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm:ss"));
        }else{
            throw new RuntimeException("时间格式不正确!");
        }
    }

    /**
     * Date类型转换为LocalDateTime
     * @param date Date
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
//        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, zoneId);

        return instant.atZone(zoneId).toLocalDateTime();
    }

    /**
     * LocalDateTime转换为Date
     * @param time LocalDateTime
     * @return Date
     */
    public static Date localDateTimeToDate(LocalDateTime time){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = time.atZone(zoneId);
        Instant instant = zonedDateTime.toInstant();
        return Date.from(instant);
    }

    /**
     * LocalDateTime转换为字符串
     * @param time LocalDateTime
     * @param pattern pattern
     * @return String
     */
    public static String localDateTimeToString(LocalDateTime time,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return time.format(formatter);
    }

    /**
     * 计算多少天后的时间 yyyy-MM-dd HH:mm:ss
     * @param time
     * @param days
     * @return
     */
    public static String addDaysToString(String time, Integer days) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);
        LocalDateTime resultDateTime = dateTime.plusDays(days);
        return formatter.format(resultDateTime);
    }

    /**
     * 计算多少天后的时间 yyyy-MM-dd
     * @param time
     * @param days
     * @return
     */
    public static String addDayToString(String time, Integer days) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dateTime = LocalDateTime.parse(time, formatter);
        LocalDateTime resultDateTime = dateTime.plusDays(days);
        return formatter.format(resultDateTime);
    }

    /**
     * 将字符串(yyyy-MM-dd HH:mm:ss)转换成毫秒的方法
     * @param dateTimeString
     * @return
     */
    public static long convertToMilliseconds(String dateTimeString) {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 解析字符串为 LocalDateTime 对象
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);

        // 转换为系统默认时区的 ZonedDateTime 对象
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

        // 获取毫秒数
        return zonedDateTime.toInstant().toEpochMilli();
    }

    /**
     * 计算两个时间差的天数 yyyy-MM-dd
     * @param firstDateString
     * @param secondDateString
     * @return
     */
    public static Integer stringDateTime2Day(String firstDateString,String secondDateString){

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate firstDate = LocalDateTime.parse(firstDateString, formatter).toLocalDate();
        LocalDate secondDate = LocalDateTime.parse(secondDateString, formatter).toLocalDate();

        Duration duration = Duration.between(firstDate.atStartOfDay(), secondDate.atStartOfDay());
        int days = (int)duration.toDays();

        return days;
    }

    /**
     * 计算两个时间差的天数 yyyy-MM-dd HH:mm:ss
     * @param firstDateString
     * @param secondDateString
     * @return
     */
    public static Integer stringDateTime2Days(String firstDateString,String secondDateString){

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDate firstDate = LocalDateTime.parse(firstDateString, formatter).toLocalDate();
        LocalDate secondDate = LocalDateTime.parse(secondDateString, formatter).toLocalDate();

        Duration duration = Duration.between(firstDate.atStartOfDay(), secondDate.atStartOfDay());
        int days = (int)duration.toDays();

        return days;
    }

    /**
     * 添加两个小时的方法
     * @param startTime
     * @return
     */
    public static String increaseTwoHour(String startTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        Date date = null;
        try {
            date = format.parse(startTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //传过来的毫秒数
        long time = date.getTime();
        //添加两个小时
        long hour = time + 2*60*60*1000;
        //转换成日期
        Date two = new Date(hour);
        //返回数据
        return format.format(two);
    }

    /**
     * 将日期转换成字符串 yyyy-MM-dd HH:mm:ss
     * @param dateTime
     * @return
     */
    public static String dateTime2String(Date dateTime){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        String time = format.format(dateTime);
        //返回数据
        return time;
    }

    /**
     * String类型yyyy-MM-dd 转换成 LocalDateTime
     * @param dateString
     * @return
     */
    public static LocalDateTime convertStringToLocalDateTime(String dateString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDateTime.parse(dateString, formatter);
    }

    /**
     * LocalDate转换为Date yyyy-MM-dd
     * @param localDate
     * @return
     */
    public static Date convertLocalDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }
}
